<?php
    class Admincp_Model_Boating extends Zend_Db_Table_Abstract
    {
        protected $_db;
		
		public function __construct()
		{
			$this->_db = Zend_Registry::get('db');
		}
        
        public function listBoating($locale)
        {
            $sql = $this->_db->select()
                            ->from('boating AS b')
                            ->join('boating_language AS bl', 'b.id = bl.boating_id', array('name'))
                            ->join('language AS l', 'bl.language_id = l.id', null)
                            ->where('l.locale = ?', $locale)
                            ->order('b.id DESC');
            return $sql;
        }
        
        public function getBoatingById($id)
        {
            $sql = $this->_db->select()
                            ->from('boating')
                            ->where('id = ?', $id);
            $result = $this->_db->fetchRow($sql);
            return $result;
        }
        
        public function getBoatingLanguageById($id)
        {
            $sql = $this->_db->select()
                            ->from('boating_language AS bl')
                            ->join('language AS l', 'bl.language_id = l.id', array('locale'))
                            ->where('bl.boating_id = ?', $id);
            $result = $this->_db->fetchAll($sql);
            return $result;
        }
        
        public function getImageById($id)
        {
            $sql = $this->_db->select()
                            ->from('boating', array('image'))
                            ->where('id = ?', $id);
            $result = $this->_db->fetchOne($sql);
            return $result;
        }
        
        public function checkBoating($name, $id = null)
        {
            $sql = $this->_db->select()
                            ->from('boating_language')
                            ->where('name = ?', $name);
            if($id != null) {
                $sql = $sql->where('boating_id <> ?', $id);
            }
            $result = $this->_db->fetchAll($sql);
            return count($result);
        }
        
        public function addBoating($data)
        {
            $this->_db->insert('boating',$data);
            return $this->_db->lastInsertId();
        }
        
        public function addBoatingLanguage($data)
        {
            $this->_db->insert('boating_language',$data);
        }
        
        public function deleteBoating($id)
        {
            $id = (int) $id;
		    $where = $this->getAdapter()->quoteInto('id = ?', $id);
            $image = $this->getImageById($id);
            if(is_file(PUBLIC_PATH . '/files/boating/' . $image)) {
                @unlink(PUBLIC_PATH . '/files/boating/' . $image);
            }
            $this->_db->delete('boating', $where);
            $where = $this->getAdapter()->quoteInto('boating_id = ?', $id);
            $this->_db->delete('boating_language', $where);
            $room = new Admincp_Model_Room();
            $image = new Admincp_Model_Image();
            $room->deleteItem($id, 2);
            $image->deleteItem($id, 2);
        }
        
        public function deleteMultiBoating($listId)
        {
            foreach($listId as $id) {
                $this->deleteBoating($id);
            }
        }
        
        public function updateBoating($data, $id)
        {
            $where = $this->getAdapter()->quoteInto('id = ?', $id);
            $this->_db->update('boating', $data, $where);
        }
        
        public function updateBoatingLanguage($data,$id)
        {
            $where = 'boating_id = '.$id.' AND language_id = '.$data['language_id'];
            $this->_db->update('boating_language', $data, $where);
        }
        
        public function searchBoating($data, $locale)
        {
            $sql = $this->_db->select()
		                    ->from('boating AS b')
                            ->join('boating_language AS bl', 'b.id = bl.boating_id', array('name'))
                            ->join('language AS l', 'bl.language_id = l.id', null)
                            ->where('l.locale = ?', $locale);
		    if($data['name'] != '') {
		        $sql = $sql->where('bl.name_plaintext LIKE ?', $data['name']);
		    }
		    if($data['status'] != 0) {
		        $sql = $sql->where('b.status = ?', $data['status']);
		    }
		    return $sql;
        }
        
        public function listStar($option = null)
        {
            $result = '';
            for($i = 1; $i <= 5; $i++) {
                if($i == $option) {
                    $result .= '<option selected="selected" value="'.$i.'">'.$i.' sao</option>';
                }
                else {
                    $result .= '<option value="'.$i.'">'.$i.' sao</option>';
                }
            }
            return $result;
        }
    }
